#include <deque>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

class Solution {
public:
  bool canVisitAllRooms(vector<vector<int>> &rooms) {
    deque<int> Keys;
    set<int> roomvisted;
    roomvisted.insert(0);
    for (auto p = rooms[0].begin(); p != rooms[0].end(); ++p) {
      Keys.push_back(*p);
    }
    while (!Keys.empty()) {
      int key = Keys.front();
      Keys.pop_front();
      if (roomvisted.find(key) == roomvisted.end()) {
	roomvisted.insert(key);
        for (auto p = rooms[key].begin(); p != rooms[key].end(); ++p) {
	  Keys.push_back(*p);
	}
      }
    }
    if(roomvisted.size() == rooms.size()){
      return true;
    }
    return false;
  }
};
